﻿using System.Collections.ObjectModel;
using System.Windows.Input;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using SoonCode.FocusTest.Api;
using SoonCode.FocusTest.Events;
using SoonCode.FocusTest.Models;
using SoonCode.FocusTest.Views;

namespace SoonCode.FocusTest.ViewModels
{
    class MainViewModel : BindableBase
    {
        public string VerName { get; } = GlobalModel.verName;
        private string _orgName;
        private int downloadProgress=0;
        private ObservableCollection<AppsModel> appsList;


        public ObservableCollection<AppsModel> AppsList
        {
            get => appsList;
            set
            {
                appsList = value;
                RaisePropertyChanged();
            }
        }

        public int DownloadProgress
        {
            get { return downloadProgress;}
            set
            {
                downloadProgress=value;
                RaisePropertyChanged();
            }
        }
        public string OrgName
        {
            get
            {
                return _orgName;
            }
            set
            {
                _orgName = value;
                RaisePropertyChanged();
            }
        }

        private IEventAggregator _eventAggregator;
        public ICommand WebviewCommand { get; set; }
        public ICommand OpenSettingCommand { get; set; }
        public ICommand OpenTestCommand { get; set; }
        public ICommand OpenMemberCommand { get; set; }
        
        

        public MainViewModel(IEventAggregator aggregator,IDialogService dialogService)
        {
            _eventAggregator = aggregator;
            aggregator.GetEvent<LoginEvent>().Subscribe(OnUserLogin);
            WebviewCommand = new DelegateCommand<string>(ViewCommand);
            OpenSettingCommand = new DelegateCommand(() =>
            {
                dialogService.ShowDialog(nameof(SettingView),null,null, "myDialogWindow");
            });
            OpenTestCommand = new DelegateCommand(() => this._eventAggregator.GetEvent<OpenTestEvent>().Publish());
            
            aggregator.GetEvent<UpgradeProgressEvent>().Subscribe(p => DownloadProgress = p);
        }


        public async void  OnUserLogin(UserInfo user)
        {
            OrgName = GlobalModel.Instance.OrgInfo.name;
            string[] apps= GlobalModel.Instance.OrgInfo.apps;
            AppsList = await API.getApps();
            ObservableCollection<AppsModel> bakApps = new ObservableCollection<AppsModel>();
            foreach (var appId in apps)
            {
                var app= AppsList.SingleOrDefault(p => p.id == appId);
                if (app!=null)
                    bakApps.Add(app);
            }
            AppsList = bakApps;
        }


        public void ViewCommand(string p)
        {
            string title="", url= API.Host + "device/";
            switch (p)
            {
                case "course":
                    title = "配课管理";
                    url += "course.html";
                    break;
                case "testResult":
                    title = "测试结果";
                    url += "testList.html";
                    break;
                case "member":
                    title = "档案管理";
                    url += "member.html";
                    break;
                case "analysis":
                    title = "数据分析";
                    url += "analysis.html";
                    break;
                case "about:blank":
                    url = "about:blank";
                    return;
            }

            WebAppModel wm = new WebAppModel() {title = title,url = $"{url}?token={GlobalModel.Instance.UserInfo.Token}"};
            this._eventAggregator.GetEvent<WebviewCommandEvent>().Publish(wm);
        }

    }
}
